<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:dssp="http://schemas.microsoft.com/xw/2004/10/dssp.html"
    xmlns:s="http://www.w3.org/2003/05/soap-envelope"
    xmlns:gpio="http://schemas.vexrobotics.com/2011/09/vexservice.gpio.html" >

  <xsl:import href="/resources/dss/Microsoft.Dss.Runtime.Home.MasterPage.xslt" />

  <xsl:template match="/">
    <xsl:comment>
      <!-- Service Header Info -->
    </xsl:comment>
    <xsl:variable name="title">
      VEX General Purpose I/O
    </xsl:variable>
    <xsl:variable name="serviceName">
      VEX General Purpose I/O
    </xsl:variable>
    <xsl:variable name="description">
      Provides access to the VEX General Purpose I/O.
    </xsl:variable>

    <xsl:call-template name="MasterPage">
      <xsl:with-param name="serviceName" select="$serviceName" />
      <xsl:with-param name="description" select="$description" />
      <!-- If title is not provided, serviceName will be used instead. -->
      <xsl:with-param name="title">
        <xsl:value-of select="$serviceName" />
        <xsl:if test="$title != ''">
          <xsl:text> - </xsl:text>
          <xsl:value-of select="$title" />
        </xsl:if>
      </xsl:with-param>
      <!-- Possible values for navigation are: 'Open', 'Closed', and 'None'
           'Open' is the default value. -->
      <xsl:with-param name="navigation" select="'Open'" />
      <!-- The contents of head param will be placed just before the </head> tag in html. -->
      <xsl:with-param name="head">
        <style type="text/css">
          /* Service-specific stylesheet goes here */
        </style>
        <script language="javascript" type="text/javascript" src="/resources/VEXService.Y2011.M09/VEXService.config.Common.js"/>
        <script language="javascript" type="text/javascript">
          <xsl:text>
var pollingFrequency = 1000;
</xsl:text>

          <![CDATA[
function onState(state)
{
    var i;
    var pins = state.Pins;
    var cnt = pins["VEXGpioPinState_Cnt"];
    for(i=1; i<=cnt; ++i)
    {
      var aPin = pins["VEXGpioPinState_" + i];
      if(aPin.PinDirection == "In")
      {
        document.all("inCell_" + i).innerText = aPin.PinState;
      }
      document.all("timestampCell_" + i).innerText = aPin.TimeStamp;
    }
    readState(pollingFrequency);
}

function startPolling()
{
  setStateCompletion("onState");
  readState(pollingFrequency);
}

setTimeout("startPolling()", pollingFrequency);

          ]]>
        </script>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="gpio:VEXGpioPinArrayState">
    <form method="POST" action="" name="SetForm">
      <input type="hidden" name="Action" value="VEXGPIO" />
      <table width="100%" border="0" cellpadding="5" cellspacing="5">
        <tr>
          <th colspan="2">
            <xsl:text disable-output-escaping="yes">VEX General Purpose I/O</xsl:text>
          </th>
        </tr>
        <xsl:apply-templates select="gpio:Pins"/>
      </table>
      <input id="Button2" type="reset" value="Reset" name="buttonReset" title="Reset Configuration"/>
      <input type="submit" value="Set Values" name="SetValues" title="Set The Output States"/>
    </form>
  </xsl:template>

  <xsl:template match="gpio:Pins/gpio:VEXGpioPinState">
    <tr>
      <table width="100%" border="0" cellpadding="5" cellspacing="5">
        <tr class="odd">
          <th width="20%">
            Name:
          </th>
          <td width="80%">
            <input type="text" name="Name" size="80">
              <xsl:attribute name="name">
                <xsl:value-of select="'Name_'"/>
                <xsl:value-of select="position()"/>
              </xsl:attribute>
              <xsl:attribute name="value">
                <xsl:value-of select="./gpio:Name"/>
              </xsl:attribute>
            </input>
          </td>
        </tr>
        <tr class="even">
          <th width="20%">
            Pin Direction:
          </th>
          <td width="80%">
            <xsl:value-of select="./gpio:PinDirection"/>
          </td>
        </tr>
        <tr class="odd">
          <th width="20%">
            Pin State:
          </th>
          <td width="80%">
            <xsl:if test ="gpio:PinDirection != 'Out'">
              <xsl:attribute name="id">
                <xsl:value-of select="'inCell_'"/>
                <xsl:value-of select="position()"/>
              </xsl:attribute>
              <xsl:value-of select="./gpio:PinState"/>
            </xsl:if>
            <xsl:if test ="gpio:PinDirection != 'In'">
              <table width="100%" border="0">
                <tr>
                  <td width="15%">
                    <xsl:attribute name="id">
                      <xsl:value-of select="'inCell_'"/>
                      <xsl:value-of select="position()"/>
                    </xsl:attribute>
                    <xsl:value-of select="./gpio:PinState"/>
                  </td>
                  <td width="15%">
                    <input id="State" type="checkbox" title="State">
                      <xsl:attribute name="name">
                        <xsl:value-of select="'output_set_'"/>
                        <xsl:value-of select="position()"/>
                      </xsl:attribute>
                      <xsl:if test="gpio:PinState != 'Low'">
                        <xsl:attribute name="checked">CHECKED</xsl:attribute>
                      </xsl:if>
                      <xsl:attribute name="value">true</xsl:attribute>
                    </input>
                  </td>
                  <td width="70%">
                    <input type="submit" value="Set">
                      <xsl:attribute name="name">
                        <xsl:value-of select="'button_set_'"/>
                        <xsl:value-of select="position()"/>
                      </xsl:attribute>
                      <xsl:attribute name="title">
                        <xsl:value-of select="'Set output '"/>
                        <xsl:value-of select="position()"/>
                      </xsl:attribute>
                    </input>
                  </td>
                </tr>
              </table>
            </xsl:if>
          </td>
        </tr>

        <tr class="even">
          <th width="20%">
            Sensor Port:
          </th>
          <td width="80%">
            <xsl:value-of select="./gpio:SensorPort"/>
          </td>
        </tr>
        <tr class="odd">
          <th width="20%">
            TimeStamp:
          </th>
          <td width="80%">
            <xsl:attribute name="id">
              <xsl:value-of select="'timestampCell_'"/>
              <xsl:value-of select="position()"/>
            </xsl:attribute>
            <xsl:value-of select="./gpio:TimeStamp"/>
          </td>
        </tr>
      </table>
    </tr>
  </xsl:template>
  
</xsl:stylesheet>
